﻿Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types
Imports System.IO
Imports DevExpress.XtraPrinting
Imports System.Xml

Public Class frmEntregaTicket
    Dim cnn As New OracleConnection(My.Settings.MiConexion)
    Private stream As New MemoryStream

    Private Sub frmEntregaTicket_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label2.Text = String.Format("{0} ({1}) - {2}, {3}, {4}", SesionActiva.Nombre, SesionActiva.Usuario,
                                    SesionActiva.municipio, SesionActiva.Sucursal, SesionActiva.Oficina)
        GenerarOpciones()
        BackColor = ColorTranslator.FromHtml("#C8F5E1")
        flpBotones.BackColor = ColorTranslator.FromHtml("#7FC9A7")
    End Sub

    Private Sub CerrarSesiónToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CerrarSesiónToolStripMenuItem.Click
        With frmLogin
            .txtUser.Text = Nothing
            .txtPass.Text = Nothing
            .Show()
        End With
        Close()
    End Sub

    Private Sub SalirToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SalirToolStripMenuItem.Click
        Close()
    End Sub

    Private Sub btnCerrar_Click(sender As Object, e As EventArgs) Handles btnCerrar.Click
        ContextMenuStrip1.Show(btnCerrar, 0, 30)
    End Sub

    Private Sub GenerarOpciones()
        'Dim gestiones As List(Of GESTIONES) = eAPPCA.gestiones(SesionActiva.idOficina)
        Dim gestiones As List(Of GESTIONES) = eAPPCA.gestionesXSO(SesionActiva.IdSucursalOficina)
        For Each g In gestiones
            'boton.ForeColor = Color.White
            Dim boton As New Button() With {.Name = String.Format("gestion{0}", g.IDGESTION),
                                            .Text = (g.NOMBRE),
                                            .Tag = g.IDGESTION,
                                            .Font = New Font("Microsoft Sans Serif", 14),
                                            .Width = 300,
                                            .Height = 120,
                                            .BackColor = SystemColors.Control}

            flpBotones.Controls.Add(boton)
            AddHandler boton.Click, AddressOf EventoClick
        Next
    End Sub

    Public Sub EventoClick(ByVal sender As Object, ByVal e As EventArgs)
        Dim boton As Button = TryCast(sender, Button)

        Try
            Dim myCMD As New OracleCommand() With {.Connection = cnn,
                                                   .CommandText = "SP_PETICION_GESTIONES",
                                                   .CommandType = CommandType.StoredProcedure}

            myCMD.Parameters.Add("VIDGESTION", OracleDbType.Decimal, 10, Nothing, ParameterDirection.Input).Value = Val(boton.Tag)
            myCMD.Parameters.Add("VIDDETALLE_SUCURSAL_OFICINA", OracleDbType.Decimal, 10, Nothing, ParameterDirection.Input).Value = SesionActiva.IdSucursalOficina
            myCMD.Parameters.Add("VTERCERAEDAD", OracleDbType.Decimal, 10, Nothing, ParameterDirection.Input).Value = IIf((chkTerceraEdad.Checked), 1, 0)
            myCMD.Parameters.Add("CODIGO", OracleDbType.NVarchar2, 10, Nothing, ParameterDirection.Output)
            myCMD.Parameters.Add("FECHA", OracleDbType.NVarchar2, 22, Nothing, ParameterDirection.Output)

            cnn.Open()
            myCMD.ExecuteNonQuery()

            'Dim r As String = String.Format("[ {0} ] - [ {1} ]", CType(myCMD.Parameters("CODIGO").Value.ToString, String), _
            'CType(myCMD.Parameters("FECHA").Value.ToString, String))
            'MsgBox("Aquí debe imprimir el ticket " & r)

            Using ticket As New rptTicket(myCMD.Parameters("CODIGO").Value.ToString, myCMD.Parameters("FECHA").Value.ToString)
                Using ps As New PrintingSystem(), stream As New MemoryStream()
                    ticket.CreateDocument()
                    ticket.PrintingSystem.SaveDocument(stream)

                    ps.LoadDocument(stream)
                    ps.ShowMarginsWarning = False
                    ps.Print()

                    ' Vista previa del reporte
                    'Using preview As New DevExpress.XtraPrinting.Preview.PrintPreviewFormEx()
                    '    preview.PrintingSystem = ps
                    '    preview.ShowDialog()
                    'End Using
                End Using
            End Using

            chkTerceraEdad.Checked = False
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString)
        Finally
            cnn.Close()
        End Try
    End Sub


    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        lblFechaHora.Text = String.Format("Hoy es {0} - {1}", Date.Today().ToString("dd MMM yyyy"), Date.Now.ToLongTimeString())
    End Sub


    Private Sub txtIdentidad_TextChanged(sender As Object, e As EventArgs) Handles txtIdentidad.TextChanged
        Dim cant As Integer = txtIdentidad.Text.Length
        If cant = 13 Then
            Try
                Dim myCMD As New OracleCommand() With {.Connection = cnn,
                                                       .CommandText = "SP_WEB_TICKET",
                                                       .CommandType = CommandType.StoredProcedure}

                myCMD.Parameters.Add("VIDENTIDAD", OracleDbType.NVarchar2, 13, Nothing, ParameterDirection.Input).Value = txtIdentidad.Text
                myCMD.Parameters.Add("VIDSUCURSAL_OFICINA", OracleDbType.Decimal, 10, Nothing, ParameterDirection.Input).Value = SesionActiva.IdSucursalOficina
                myCMD.Parameters.Add("EXISTE", OracleDbType.Decimal, 10, Nothing, ParameterDirection.Output)
                myCMD.Parameters.Add("TICKET", OracleDbType.NVarchar2, 10, Nothing, ParameterDirection.Output)
                myCMD.Parameters.Add("FECHA", OracleDbType.NVarchar2, 22, Nothing, ParameterDirection.Output)
                myCMD.Parameters.Add("EXISTEOOF", OracleDbType.Decimal, 10, Nothing, ParameterDirection.Output)
                myCMD.Parameters.Add("SUCURSAL", OracleDbType.NVarchar2, 30, Nothing, ParameterDirection.Output)
                myCMD.Parameters.Add("OFICINA", OracleDbType.NVarchar2, 30, Nothing, ParameterDirection.Output)

                cnn.Open()
                myCMD.ExecuteNonQuery()

                'Dim r As String = String.Format("[ {0} ] - [ {1} ]", CType(myCMD.Parameters("CODIGO").Value.ToString, String), _
                'CType(myCMD.Parameters("FECHA").Value.ToString, String))
                'MsgBox("Aquí debe imprimir el ticket " & r)
                If myCMD.Parameters("EXISTE").Value = 1 Then
                    Using ticket As New rptTicket(myCMD.Parameters("TICKET").Value.ToString, myCMD.Parameters("FECHA").Value.ToString)
                        Using ps As New PrintingSystem(), stream As New MemoryStream()
                            ticket.CreateDocument()
                            ticket.PrintingSystem.SaveDocument(stream)

                            ps.LoadDocument(stream)
                            ps.ShowMarginsWarning = False
                            ps.Print()

                            ' Vista previa del reporte
                            'Using preview As New DevExpress.XtraPrinting.Preview.PrintPreviewFormEx()
                            '    preview.PrintingSystem = ps
                            '    preview.ShowDialog()
                            'End Using
                        End Using
                    End Using

                ElseIf myCMD.Parameters("EXISTEOOF").Value = 1 Then
                    MsgBox(String.Format("El ticket solicitado pertenece a {0}Sucursal: {1} {2}Oficina: {3}",
                                         vbCrLf, myCMD.Parameters("SUCURSAL").Value.ToString, vbCrLf, myCMD.Parameters("OFICINA").Value.ToString))
                Else
                    MsgBox(String.Format("No hay ticket para la identidad ingresada"))
                End If

                chkTerceraEdad.Checked = False
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString)
            Finally
                cnn.Close()
            End Try
            txtIdentidad.Text = ""
        End If
    End Sub

End Class